﻿Public Class frmClub
    Dim consulta As New consultas
    Dim ClubçBuscar As New ClubBuscar
    Dim valida As New Validations
    Dim id As String
    Dim id_dire As String


    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        ClubBuscar.dgv_club.DataSource = consulta.Consultar("club")
        ClubBuscar.Show()

    End Sub

    Private Sub btnSalir_Click(sender As System.Object, e As System.EventArgs) Handles btnSalir.Click
        Dim Respuesta As Integer = MessageBox.Show("¿Esta seguro que quiere salir?", "Confirmar", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If Respuesta = 1 Then
            End
        End If
    End Sub


    Private Sub frmClub_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.id = -1
        habilitaBotones(True)
        habilitaLabels(False)
    End Sub

    Public Sub habilitaBotones(ByVal param As Boolean)
        Me.btnBuscar.Enabled = param
        Me.btnSalir.Enabled = param
        Me.btnNuevo.Enabled = param
        Me.btnGuardar.Enabled = Not (param)
        Me.btnCancelar.Enabled = Not (param)
        Me.btnEliminar.Enabled = Not (param)
        Me.btnAgregarImagen.Visible = Not (param)
    

    End Sub

    Public Sub habilitaLabels(ByVal param As Boolean)
        Me.habilitaTextbox(param)

        Me.txtCalle.Text = ""
        Me.txtCodPostal.Text = ""
        Me.txtCuit.Text = ""
        Me.txtDpto.Text = ""
        Me.txtNombreClub.Text = ""
        Me.txtNro.Text = ""
        Me.txtPiso.Text = ""
        Me.dtpFechaFundacion.Text = ""
        Me.cmbEstadoCancha.Text = ""
        Me.cmbTipoPiso.Text = ""
    End Sub

    Public Sub habilitaTextbox(ByVal param As Boolean)
        Me.txtCalle.Enabled = param
        Me.txtCodPostal.Enabled = param
        Me.txtCuit.Enabled = param
        Me.txtDpto.Enabled = param
        Me.txtNombreClub.Enabled = param
        Me.txtNro.Enabled = param
        Me.txtPiso.Enabled = param
        Me.dtpFechaFundacion.Enabled = param
        Me.cmbEstadoCancha.Enabled = param
        Me.cmbTipoPiso.Enabled = param
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        Me.id = -1
        habilitaLabels(True)
        habilitaBotones(False)
        Me.btnEliminar.Enabled = False
    End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        Dim valida As New Validations
        Dim domicilio As New Domicilio
        Dim club As New ClubBean


        If (valida.validarNoNull(Me)) Then
            If (valida.validaComboBox(Me)) Then
                If (valida.validaDatePickers(Me)) Then
                    If (Me.id = -1) Then
                        Try
                            domicilio.id_direccionInfo = Me.id_dire
                            domicilio.calleInfo = Me.txtCalle.Text
                            domicilio.numeroInfo = Me.txtNro.Text
                            domicilio.codigoPostalInfo = Me.txtCodPostal.Text
                            domicilio.deptoInfo = Me.txtDpto.Text
                            domicilio.pisoInfo = Me.txtPiso.Text

                            club.nombreInfo = Me.txtNombreClub.Text
                            club.cuitInfo = Me.txtCuit.Text
                            club.domicilioInfo = domicilio
                            club.fechaFundacion = Me.dtpFechaFundacion.Text
                            club.estadoCanchaInfo = Me.cmbEstadoCancha.SelectedItem
                            club.tipoPisoInfo = Me.cmbTipoPiso.SelectedItem
                            MsgBox(club.domicilioInfo.id_direccionInfo)

                            consulta.InsertClub(club)
                            habilitaBotones(True)
                            habilitaLabels(False)
                        Catch
                        End Try

                    Else
                        Try
                            club.idInfo = Me.id
                            consulta.ModificarClub(club)
                        Catch
                        End Try

                        habilitaBotones(True)
                        habilitaLabels(False)
                    End If
                End If
            End If
        End If


    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        valida.resetCheckBox(Me)
        valida.resetComboBox(Me)
        valida.resetRdButton(Me)
        valida.resetTextBox(Me)
        habilitaBotones(True)
        habilitaLabels(False)
    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click
        If (Me.id = -1) Then
            MessageBox.Show("Primero debe seleccionar un item a editar", "Atencion", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        Else
            If (MessageBox.Show("Estas seguro que queres eliminarlo?", "Advertencia", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = 6) Then
                consulta.Eliminar("club", Me.id)
                habilitaBotones(True)
                habilitaLabels(False)
                Me.id = -1
                ClubBuscar.dgv_club.DataSource = consulta.Consultar("club")
            End If
        End If
        
        
    End Sub
    'Getters & Setters
    Public Property idInfo() As Integer
        Get
            Return id
        End Get
        Set(ByVal value As Integer)
            Me.id = value
        End Set
    End Property

    Public Property id_direInfo() As Integer
        Get
            Return id
        End Get
        Set(ByVal value As Integer)
            Me.id_dire = value
        End Set
    End Property


End Class